package com.sgb.zhaocai.widget.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.sgb.link.utils.NumericalTransformationTool;
import com.sgb.zhaocai.BR;
import com.sgb.zhaocai.R;
import com.sgb.zhaocai.entity.PickExpertDetailEntity;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.PickSelectEntity;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 专家库-执业资格证书-适配器
 */
public class PractisingCertificateAdapter extends BaseRecycleAdapter<PickExpertDetailEntity.Qualifications> {

    private Callback callback;
    private List<PickSelectEntity> certificateList;
    private Context context;
    private List<PickExpertDetailEntity.Qualifications> list;
    private ItemInputTextView tvCertificateNumber;

    public PractisingCertificateAdapter(Context context, List<PickExpertDetailEntity.Qualifications> list) {
        super(context, R.layout.item_practising_certificate, list);
        this.context = context;
        this.list = list;
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, @SuppressLint("RecyclerView") int position) {
        super.onBindViewHolder(holder, position);
        ImageView delete = holder.itemView.findViewById(R.id.delete);//删除
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.delete(position);
            }
        });
        TextView tvSort = holder.itemView.findViewById(R.id.tv_sort);
        tvSort.setText("(" + NumericalTransformationTool.getNumberText(position + 1) + ")");

        ItemInputTextView tvTime = holder.itemView.findViewById(R.id.tv_time);
        tvTime.setOnWholeItemClickListener(new ItemInputTextView.OnWholeItemClickListener() {
            @Override
            public void OnWholeItemClick(View view) {
                Calendar dayCalendar = Calendar.getInstance();
                dayCalendar.add(Calendar.DAY_OF_MONTH, 0);
                TimePickerView timePicker = new TimePickerBuilder(context, (date, v) -> {
                    @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    String format = sdf.format(date);
                    tvTime.setContent(format);
                    list.get(position).setTime(DateUtils.getTimeStamp(format,"yyyy-MM-dd"));

                }).setType(new boolean[]{true, true, true, false, false, false})
                        .setCancelText("取消").setSubCalSize(15)
                        .setCancelColor(Color.parseColor("#666666"))
                        .setTitleText("取得时间")
                        .setTitleColor(Color.parseColor("#969696")).setTitleSize(15)
                        .setSubmitText("确定")
                        .setSubmitColor(Color.parseColor("#5792FD"))
                        .setRangDate(null, dayCalendar)
                        .build();
                timePicker.show();
            }
        });
        tvTime.setDeliverLineVG(false);
        ItemInputTextView tvCertificateName = holder.itemView.findViewById(R.id.tv_certificate_name);
        tvCertificateName.setOnWholeItemClickListener(new ItemInputTextView.OnWholeItemClickListener() {
            @Override
            public void OnWholeItemClick(View view) {
                if (certificateList == null) {
                    return;
                }
                OptionsPickerView<PickSelectEntity> mCustomerOptions = new OptionsPickerBuilder(context, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        tvCertificateName.setContent(certificateList.get(options1).getProjectscale());
                        list.get(position).setCertificateName(tvCertificateName.getContent());
                    }
                }).setCancelColor(Color.parseColor("#666666"))
                        .setSubCalSize(15)
                        .setTitleColor(Color.parseColor("#969696")).setTitleSize(15)
                        .setSubmitColor(Color.parseColor("#5792FD")).build();
                mCustomerOptions.setTitleText("执业资格证书名称");
                mCustomerOptions.setPicker(certificateList);
                mCustomerOptions.setSelectOptions(0);
                mCustomerOptions.show();
            }
        });

        tvCertificateNumber = holder.itemView.findViewById(R.id.tv_certificate_number);
        tvCertificateNumber.setInfilterType(ItemInputTextView.TYPE_INDO);
        tvCertificateNumber.onTextChange(new ItemInputTextView.onTextChange() {
            @Override
            public void change(String s) {
                Pattern p = Pattern.compile("[^a-zA-Z0-9]");
                Matcher m = p.matcher(s);
                String regEx = m.replaceAll("").trim();
                list.get(position).setNumber(regEx);
            }
        });

        if(list.get(position) != null){
            tvCertificateNumber.setContent(list.get(position).getNumber());
            tvCertificateName.setContent(list.get(position).getCertificateName());
            tvTime.setContent(list.get(position).getTime());
        }
    }

    @Override
    public void convert(ItemViewHolder holder, PickExpertDetailEntity.Qualifications entity) {
        holder.setBinding(BR.viewModel, entity);
    }

    public interface Callback {
        void delete(int position);
    }

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    public void setCertificateData(List<PickSelectEntity> data) {
        certificateList = data;
    }

}
